package com.atfusu.service.impl;

import com.atfusu.base.BaseDao;
import com.atfusu.base.impl.BaseServiceImpl;
import com.atfusu.bean.entity.Permission;
import com.atfusu.bean.entity.RolePermission;
import com.atfusu.bean.helper.PermissionHelper;
import com.atfusu.dao.PermissionDao;
import com.atfusu.dao.RolePermissionDao;
import com.atfusu.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Title:
 * Author: fusu
 * DateTime:  2022/12/4
 * Description:
 **/
@DubboService
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    protected BaseDao<Permission> getEntityDao() {
        return permissionDao;
    }

    @Override
    public List<Map<String, Object>> findPermissionByRoleId(Long roleId) {
        //①获取全部权限列表
        List<Permission> permissionList = permissionDao.findAll();
        //②查找roleId具有的权限
        List<Long> permissionIds = rolePermissionDao.findPermissionByRoleId(roleId);
        //③构建ztree数据（{ id:2, pId:0, name:"随意勾选 2", checked:true, open:true）
        List<Map<String, Object>> zNodes = new ArrayList<>();
        for (Permission permission : permissionList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", permission.getId());
            map.put("pId", permission.getParentId());
            map.put("name", permission.getName());
            if (permissionIds.contains(permission.getId())) {
                map.put("checked", true);
            }
            zNodes.add(map);
        }
        return zNodes;
    }

    @Override
    @Transactional
    public void addPermissionRoleRelationShip(Long roleId, Long[] permissionIds) {
        //①将已有的权限删除
        rolePermissionDao.deleteByRoleId(roleId);
        //②循环添加分配的权限
        for (Long permissionId : permissionIds) {
            if (permissionId != null) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPermissionId(permissionId);
                rolePermission.setRoleId(roleId);
                rolePermissionDao.insert(rolePermission);
            }
        }
    }

    @Override
    public List<Permission> getPermissionByAdmin(Long id) {
        //1.根据adminId查询所有权限
        List<Permission> permissionList = null;
        if (id == 1) {
            //设置超级管理员，拥有所有权限
            permissionList = permissionDao.findAll();
        } else {
            permissionList = permissionDao.getPermissionByAdmin(id);
        }
        //2.根据权限级别进行分级处理
        return PermissionHelper.bulid(permissionList);
    }


    @Override
    public List<Permission> findAll() {
        List<Permission> permissionList = permissionDao.findAll();
        if (CollectionUtils.isEmpty(permissionList)) {
            return null;
        }
        return PermissionHelper.bulid(permissionList);
    }

    /**
     * 如果删除的菜单有下级，则需要一并删除
     */
    @Override
    public void delete(Serializable id) {
        //1.查询是否有子级
        List<Permission> permissionList = permissionDao.findPermissionByParentId(id);
        permissionDao.delete(id);
        if (CollectionUtils.isEmpty(permissionList)) {
            return;
        } else {
            for (Permission permission : permissionList) {
                delete(permission.getId());
            }
        }
    }

    /**
     * 根据adminId查询用户拥有的三级权限（增删改查之类的权限）
     */
    @Override
    public List<String> findPermissionCodeByAdminId(Long adminId) {
        List<String> codes = null;
        if (adminId == 1) {
            //给超级管理员分配所有权限
            codes = permissionDao.findAllCode();
        } else {
            codes = permissionDao.findCodeByAdminId(adminId);
        }
        return codes;
    }
}
